package com.ujiuye.servlet;

import com.ujiuye.entity.ResultVO;
import com.ujiuye.entity.User;
import com.ujiuye.service.UserService;
import com.ujiuye.service.impl.UserServiceImpl;
import com.ujiuye.util.JsonUtils;
import com.ujiuye.util.PageBean;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    UserService us = new UserServiceImpl();

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String imageCode = req.getParameter("imageCode");

        ServletContext servletContext = req.getServletContext();
        String verCode = (String) servletContext.getAttribute("verCode");

        PrintWriter pw = resp.getWriter();
        User user = us.checkUser(username,password);
        servletContext.setAttribute("user",user);
        if (verCode.equalsIgnoreCase(imageCode)){
            if (user!=null){
                if (user.getStatus()==1 && user.getRole()== 1){
                    String json = JsonUtils.toJson(new ResultVO(4,"登录成功",null),resp);
                    pw.print(json);
                }else {
                    String json = JsonUtils.toJson(new ResultVO(3,"权限不足",null),resp);
                    pw.print(json);
                }
            }else {
                String json = JsonUtils.toJson(new ResultVO(2,"用户名或者密码有误",null),resp);
                pw.print(json);
            }
        }else {
            String json = JsonUtils.toJson(new ResultVO(1,"验证码有误",null),resp);
            pw.print(json);
        }
    }

    public void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = req.getServletContext();
        User user = (User)servletContext.getAttribute("user");
        System.out.println(user);
        String json="";
        if (user!=null){
            json = JsonUtils.toJson(new ResultVO(200,"获取用户成功",user),resp);

        }else {
            json = JsonUtils.toJson(new ResultVO(500,"获取用户失败",user),resp);

        }
        resp.getWriter().print(json);
    }

    public void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = req.getServletContext();
        servletContext.removeAttribute("user");
        String json = JsonUtils.toJson(new ResultVO(200, "成功退出", null), resp);
        resp.getWriter().print(json);
    }
    //查询所有用户
    public void findAllByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取想看的页码
        String currentPage = req.getParameter("currentPage");
        //获取每页显示条数
        String pageSize = req.getParameter("pageSize");
        //获取查询参数
        String name = req.getParameter("name");
        //封裝pageBaan
        PageBean<User> pb = us.findAllByPage(currentPage,pageSize,name);
        //將pb转为json
        String json = JsonUtils.toJson(pb, resp);
        resp.getWriter().print(json);
    }
    //添加用户
    public void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取用户的map集合
        Map<String, String[]> map = req.getParameterMap();
        //2:创建user对象
        User user = new User();
        //3：将map集合中的数据封装到user对象中
        BeanUtils.populate(user,map);
        //4:调用service
        int num = us.addUser(user);
        //5:给出响应
        ResultVO vo = null;
        if(num>0){
            vo = new ResultVO(200,"添加用户成功",null);
        }else{
            vo = new ResultVO(500,"添加用户失败",null);
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);
    }

    public void delAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取uids数据
        String uids = req.getParameter("uids");
        //调用service
        int num = us.delAll(uids);
        //给出响应
        ResultVO vo = null;
        if (num>0){
            vo = new ResultVO(200,"删除成功",null) ;
        }else {
            vo = new ResultVO(500,"删除失败",null) ;
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);
    }
    //修改用户
    public void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取用户数据
        Map<String, String[]> map = req.getParameterMap();
        //创建user对象
        User user = new User();
        BeanUtils.populate(user,map);
        //调用service
        int num = us.updateUser(user);
        //给出响应
        ResultVO vo = null;
        if (num>0){
            vo = new ResultVO(200,"修改成功",null) ;
        }else {
            vo = new ResultVO(500,"修改失败",null) ;
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);
    }
    //检测手机号码是否可用
    public void checkPhone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取手机用户
        String tel = req.getParameter("tel");

        //调用service
        User user = us.checkPhone(tel);
        //给出响应
        ResultVO vo = null;
        if (user!=null){
            vo = new ResultVO(200,"手机号码已被注册",null) ;
        }else {
            vo = new ResultVO(500,"手机号码可用",null) ;
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);
    }

    //前台注册用户
    public void registerUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户信息
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");

        //调用service
        int num = us.registerUser(name,password,phone);
        //给出响应
        ResultVO vo = null;
        if (num>0){
            vo = new ResultVO(200,"注册成功",null) ;
        }else {
            vo = new ResultVO(500,"注册失败",null) ;
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);
    }

    //前台登录用户
    protected void beforeLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1:获取用户的信息
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        //2:调用service
        User user =  us.beforeLogin(password,phone);
        //4:给出响应
        ResultVO vo = null;
        if(user!=null){
            vo = new ResultVO(200,"登录成功",user);
        }else{
            vo = new ResultVO(500,"登录失败",null);
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);

    }





}

